package thoven.achievement.net.jsonutil.ui

import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.JBTextArea
import com.intellij.ui.components.JBTextField
import com.intellij.util.ui.JBUI
import thoven.achievement.net.jsonutil.service.JsonPathQuery
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import javax.swing.*

/**
 * JSONPath 查询对话框
 */
class JsonPathQueryDialog(
    private val project: Project,
    private val jsonContent: String
) : DialogWrapper(project) {
    
    private val pathField = JBTextField()
    private val resultArea = JBTextArea()
    private val examplesCombo = JComboBox<String>()
    
    init {
        title = "JSONPath 查询"
        init()
        setupUI()
    }
    
    override fun createCenterPanel(): JComponent {
        val panel = JPanel(GridBagLayout())
        val gbc = GridBagConstraints()
        gbc.fill = GridBagConstraints.HORIZONTAL
        gbc.insets = JBUI.insets(5)
        gbc.weightx = 1.0
        
        // JSONPath 输入
        gbc.gridx = 0
        gbc.gridy = 0
        panel.add(JBLabel("JSONPath 表达式:"), gbc)
        
        gbc.gridy = 1
        pathField.text = "$"
        panel.add(pathField, gbc)
        
        // 查询按钮
        gbc.gridy = 2
        val queryButton = JButton("查询").apply {
            addActionListener { performQuery() }
        }
        panel.add(queryButton, gbc)
        
        // 常用示例
        gbc.gridy = 3
        panel.add(JBLabel("常用示例:"), gbc)
        
        gbc.gridy = 4
        val examples = JsonPathQuery.getExamples()
        examples.forEach { (path, desc) ->
            examplesCombo.addItem("$path - $desc")
        }
        examplesCombo.addActionListener {
            val selected = examplesCombo.selectedItem as? String
            if (selected != null) {
                val path = selected.substringBefore(" - ")
                pathField.text = path
            }
        }
        panel.add(examplesCombo, gbc)
        
        // 结果区域
        gbc.gridy = 5
        panel.add(JBLabel("查询结果:"), gbc)
        
        gbc.gridy = 6
        gbc.weighty = 1.0
        gbc.fill = GridBagConstraints.BOTH
        resultArea.isEditable = false
        resultArea.lineWrap = true
        resultArea.wrapStyleWord = true
        val scrollPane = JBScrollPane(resultArea)
        scrollPane.preferredSize = Dimension(600, 400)
        panel.add(scrollPane, gbc)
        
        return panel
    }
    
    private fun setupUI() {
        // 自动执行一次默认查询
        SwingUtilities.invokeLater {
            performQuery()
        }
    }
    
    private fun performQuery() {
        val path = pathField.text.trim()
        
        if (path.isEmpty()) {
            resultArea.text = "请输入 JSONPath 表达式"
            return
        }
        
        val result = JsonPathQuery.query(jsonContent, path)
        
        if (result.isSuccess) {
            resultArea.text = result.getOrNull()
        } else {
            resultArea.text = "查询失败: ${result.exceptionOrNull()?.message}"
        }
    }
    
    override fun createActions(): Array<Action> {
        return arrayOf(okAction)
    }
}

